package com.evan.review.viewpager;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.evan.review.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

/**
 * ViewPager懒加载演示Activity
 * 展示了如何在ViewPager中实现懒加载模式
 */
public class LazyLoadingViewPagerActivity extends AppCompatActivity {
    
    private static final String TAG = "LazyLoadingViewPager";
    private static final int PAGE_COUNT = 5; // 页面数量
    
    private ViewPager2 viewPager; // ViewPager2控件
    private TabLayout tabLayout; // 顶部标签页
    private TextView tvExplanation; // 解释说明文本
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lazy_loading_viewpager);
        
        initViews(); // 初始化视图
        setupViewPager(); // 设置ViewPager
    }
    
    /**
     * 初始化视图组件
     */
    private void initViews() {
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.tab_layout);
        tvExplanation = findViewById(R.id.tv_explanation);
        
        StringBuilder explanation = new StringBuilder();
        explanation.append("懒加载演示：\n\n");
        explanation.append("• ViewPager2会在页面进入视图或在离屏页面缓存范围内时创建Fragment\n");
        explanation.append("• LazyFragment实现了Fragment.setUserVisibleHint()和Fragment.setMaxLifecycle()来控制数据加载\n");
        explanation.append("• 适配器使用RESUME_ONLY_CURRENT_FRAGMENT行为来优化资源使用\n\n");
        explanation.append("查看Logcat以观察Fragment生命周期和懒加载过程中的方法调用");
        
        tvExplanation.setText(explanation.toString());
    }
    
    /**
     * 设置ViewPager及其适配器
     */
    private void setupViewPager() {
        LazyFragmentStateAdapter adapter = new LazyFragmentStateAdapter(this);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(1); // 默认行为
        
        // 将TabLayout与ViewPager关联
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            tab.setText("页面 " + (position + 1));
        }).attach();
        
        // 注册页面变化回调
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Log.d(TAG, "onPageSelected: " + position);
            }
        });
    }
    
    /**
     * 懒加载Fragment适配器
     */
    private class LazyFragmentStateAdapter extends FragmentStateAdapter {
        
        // 各页面的背景颜色
        private final int[] COLORS = new int[] {
            Color.parseColor("#F44336"), // 红色
            Color.parseColor("#9C27B0"), // 紫色
            Color.parseColor("#3F51B5"), // 靛蓝色
            Color.parseColor("#4CAF50"), // 绿色
            Color.parseColor("#FF9800")  // 橙色
        };
        
        LazyFragmentStateAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }
        
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            Log.d(TAG, "createFragment: 位置=" + position);
            return LazyFragment.newInstance(position + 1, COLORS[position]);
        }
        
        @Override
        public int getItemCount() {
            return PAGE_COUNT;
        }
    }
} 